package cl.whyem.spring.integration.mail.app;

import cl.whyem.spring.integration.mail.config.RootConfig;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BootstrapApp {

    private final static Logger logger = LoggerFactory.getLogger(BootstrapApp.class);

    public static void main(String[] args) throws javax.mail.MessagingException, InterruptedException {
        final ApplicationContext context = new AnnotationConfigApplicationContext(RootConfig.class);
        logger.info("*** Iniciando proceso desuscripcion correos :: [{}] ***", new Date(context.getStartupDate()));
        CountDownLatch latch = context.getBean("countDownLatch", CountDownLatch.class);

        //Hilo procesamiento mails
        ProcesadorMail procesadorMail = context.getBean("procesadorMail", ProcesadorMail.class);
        Thread procesador = new Thread(procesadorMail);
        procesador.start();

        long threadId = Thread.currentThread().getId();
        logger.debug("Thread # " + threadId + " is doing this task");

        try {
            logger.debug("waiting...");
            latch.await();
        } catch (InterruptedException ex) {
            logger.error("Error: {}", ex.getMessage(), ex);
        } finally {
            logger.info("*** Terminando proceso desuscripcion correos :: [{}] ***", new Date());
            System.exit(0);
        }
    }

}
